<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\UserAddressRequest;
use App\Models\UserAddress;
use Illuminate\Http\Request;

class UserAddressesController extends Controller
{
    /**
     * 收货地址列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        return $this->success($request->user()->addresses);
    }

    /**
     * 默认收货地址
     * @param Request $request
     * @return mixed
     */
    public function defaultShow(Request $request)
    {
        $defaultAddress = $request->user()->addresses()->orderBy('last_used_at', 'desc')->first();
        return $this->success($defaultAddress);
    }

    public function show(UserAddress $userAddress)
    {
        $this->authorize('own', $userAddress);
        return $this->success($userAddress);
    }

    /**
     * 添加地址
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserAddressRequest $request)
    {
        $user = request()->user();

        $data = $request->only([
            'province',
            'city',
            'district',
            'address',
            'zip',
            'contact_name',
            'contact_phone',
            'is_default'
        ]);
        $data['is_default'] = boolval($data['is_default']);

        if ($data['is_default']) {
            $user->addresses()->get()->map(function ($value) {
                $value->update([
                    'is_default' => false
                ]);
            });
        }

        if ($user->addresses->count() > 6) {
            return $this->failed('最多只能有6个地址');
        }

        $user->addresses()->create($data);

        return $this->message('添加地址成功');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UserAddress $userAddress, UserAddressRequest $request)
    {
        $this->authorize('own', $userAddress);

        $data = $request->only([
            'province',
            'city',
            'district',
            'address',
            'zip',
            'contact_name',
            'contact_phone',
            'is_default'
        ]);
        $data['is_default'] = boolval($data['is_default']);

        if ($data['is_default']) {
            $request->user()->addresses()->get()->map(function ($value) {
                $value->update([
                    'is_default' => false
                ]);
            });
        }

        $userAddress->update($data);

        return $this->message('修改地址成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(UserAddress $userAddress)
    {
        $this->authorize('own', $userAddress);

        $userAddress->delete();

        return $this->message('删除地址成功');
    }
}
